package com.lucenly.card.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.lucenly.card.R;
import com.siberiadante.toastutils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelfareAdapter extends PagerAdapter {

    private Context mContext;
    private List<Integer> dataList = new ArrayList<>();

    public WelfareAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setDatas(List<Integer> list) {
        if (list.size() <= 0) {
            dataList.clear();
            notifyDataSetChanged();
            return;
        }
        dataList.clear();
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return /*Integer.MAX_VALUE*/dataList.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

            Integer data = dataList.get(position);

            ViewHolder viewHolder = null;
            View view = LayoutInflater.from(mContext).inflate(
                    R.layout.view_card_item, null);
            if (viewHolder == null) {
                viewHolder = new ViewHolder(view);
            }
            bindView(viewHolder, data,position);

            container.addView(view, LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);

            return view;
    }

    private void bindView(final ViewHolder viewholder, Integer data, final Integer position) {
//        Glide.with(mContext).load(data.pic).into(viewholder.welfareImage);
//        viewholder.welfareImage.setBackgroundResource(data);
        switch (position){
            case 0:
                viewholder.iv_title.setImageResource(R.mipmap.ic_zq);
                viewholder.welfareImage.setImageResource(R.mipmap.zq_right);
                break;
            case 1:
                viewholder.iv_title.setImageResource(R.mipmap.ic_ws);
                viewholder.welfareImage.setImageResource(R.mipmap.ws_right);
                break;
            case 2:
                viewholder.iv_title.setImageResource(R.mipmap.ic_zs);
                viewholder.welfareImage.setImageResource(R.mipmap.zs_right);
                break;
        }
        viewholder.welfareImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                ProductDetailsActivity.open(mContext);
                ToastUtil.showSingletonShort(position+"");
            }
        });

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public class ViewHolder {
        @BindView(R.id.iv_right)
        ImageView welfareImage;
        @BindView(R.id.iv_title)
        ImageView iv_title;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }

        public void reset() {
            welfareImage.setImageResource(R.mipmap.banner_bg2x);
        }
    }

}

